package com.zadetk.helpyou.fragments;

import android.content.Intent;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zadetk.helpyou.App;
import com.zadetk.helpyou.R;
import com.zadetk.helpyou.activities.EvaluateActivity;
import com.zadetk.helpyou.base.AbsListFragment;
import com.zadetk.helpyou.bean.MydemandBean;
import com.zadetk.helpyou.net.BaseObserver;
import com.zadetk.helpyou.net.BaseResponse;
import com.zadetk.helpyou.net.NetTool;
import com.zadetk.helpyou.other.Const;
import com.zadetk.helpyou.other.UserInfoManger;
import com.zadetk.helpyou.utils.LogUtil;
import com.zadetk.helpyou.utils.ToastUtil;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Zackv on 2018/5/11.
 */

public class MyDemandFragment extends AbsListFragment {
    private MyDemandAdapter robAdapter;
    private List<MydemandBean> data = new LinkedList<>();

    @Override
    protected void firstRequestData() {
        getMydemand();
    }

    @Override
    protected void loadMore() {
//        if (data.size() > 20) {
            robAdapter.loadMoreEnd();
//            return;
//        }
//        MydemandBean robItemBean = new MydemandBean();
//        data.add(robItemBean);
//        data.add(robItemBean);
//        data.add(robItemBean);
//        data.add(robItemBean);
//        robAdapter.notifyDataSetChanged();
//        robAdapter.loadMoreComplete();
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        robAdapter = new MyDemandAdapter(R.layout.myorder_item1, data);
        robAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_confirm:
                        break;
                    case R.id.tv_reject:
                        break;
                    case R.id.tv_complete:
                        break;
                    case R.id.tv_unpay:
                        break;
                    case R.id.tv_pay:
                        payRequest();
                        break;
                    case R.id.tv_evaluate:
                        startActivity(new Intent(mActivity, EvaluateActivity.class));
                        break;
                    case R.id.tv_cancel:
                        cancelRequest(data.get(position));
                        break;
                    case R.id.iv_call:
                        call("13708079634");
                        break;
                    case R.id.iv_message:
                        final String target = "testpro2"; //消息接收者ID
                        final String appkey = "23015524"; //消息接收者appKey
                        Intent intent = App.mIMKit.getChattingActivityIntent(target, appkey);
                        startActivity(intent);
                        break;

                }
            }
        });
        return robAdapter;
    }

    private void payRequest() {
        ToastUtil.showToast("支付请求！");
    }

    private void call(String phoneNumber) {
        Uri uri = Uri.parse("tel:" + phoneNumber);
        Intent intent = new Intent(Intent.ACTION_CALL, uri);
        startActivity(intent);
    }

    private void cancelRequest(MydemandBean itemBean) {
        ToastUtil.showToast("取消订单！");
    }

    @Override
    protected void OnItemClick(int position) {

    }

    @Override
    protected void OnRefresh() {
        setRefreshing(false);
    }

    private class MyDemandAdapter extends BaseQuickAdapter<MydemandBean, BaseViewHolder> {

        public MyDemandAdapter(int layoutResId, @Nullable List<MydemandBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, MydemandBean item) {
            helper.addOnClickListener(R.id.tv_confirm);
            helper.addOnClickListener(R.id.tv_reject);
            helper.addOnClickListener(R.id.tv_complete);
            helper.addOnClickListener(R.id.tv_unpay);
            helper.addOnClickListener(R.id.tv_done);
            helper.addOnClickListener(R.id.tv_pay);
            helper.addOnClickListener(R.id.tv_evaluate);
            helper.addOnClickListener(R.id.tv_cancel);
            helper.addOnClickListener(R.id.iv_call);
            helper.addOnClickListener(R.id.iv_message);
            helper.getView(R.id.tv_confirm).setVisibility(View.GONE);
            helper.getView(R.id.tv_reject).setVisibility(View.GONE);
            helper.getView(R.id.tv_complete).setVisibility(View.GONE);
            helper.getView(R.id.tv_unpay).setVisibility(View.GONE);
            helper.getView(R.id.tv_done).setVisibility(View.GONE);
            helper.getView(R.id.tv_pay).setVisibility(View.GONE);
            helper.getView(R.id.tv_evaluate).setVisibility(View.GONE);
            helper.getView(R.id.tv_cancel).setVisibility(View.GONE);

            switch (item.getOrder_status()) {
                case Const.OREDER_STATUS_DONE:
                    break;
                case Const.OREDER_STATUS_CARRY_ON:
                    break;
                case Const.OREDER_STATUS_UNEVALUATE:
                    helper.setVisible(R.id.tv_evaluate, true);
                    helper.setText(R.id.tv_order_status, "待评价");
                    break;
                case Const.OREDER_STATUS_UNPAY:
                    helper.setVisible(R.id.tv_pay, true);
                    helper.setText(R.id.tv_order_status, "去支付");

                    break;
                case Const.OREDER_STATUS_WAIT_CONFIRM:
                    helper.setVisible(R.id.tv_cancel, true);
                    helper.setText(R.id.tv_order_status, "待确认");
                    break;
            }

            //x add
            helper.setText(R.id.tv_order_name, item.getOrder_name());
            helper.setText(R.id.tv_order_time, item.getAdd_time());
            helper.setText(R.id.tv_order_distance, item.getDistance());
            helper.setText(R.id.tv_order_address, item.getWorker_addr());
            helper.setText(R.id.tv_phonenumber, item.getWorker_tel());
        }
    }

    /**
     * 我的需求列表
     */
    private void getMydemand() {
        Map<String, Object> param = NetTool.newParams();
        param.put("uid", UserInfoManger.userInfo.getUid());
        param.put("token", UserInfoManger.userInfo.getUserToken());
        NetTool.getApi().getMydemand(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<MydemandBean>>>() {
                    @Override
                    public void onData(BaseResponse<List<MydemandBean>> value) {
                        if (value.getCode() == 0) {
                            data.clear();
                            List<MydemandBean> user = value.getData();
                            data.addAll(user);
                            for (MydemandBean bean : user) {
                                LogUtil.e(bean.toString());
                            }
                            robAdapter.notifyDataSetChanged();
                            LogUtil.e(user + " " + value.getMessage());
                        } else {
                            LogUtil.e(value.error_code + " error code");
                        }
                    }
                });
    }
}
